
var id=""
var seasonsids = ""
var count =0
var price = 0
var containbuyed
Page({

  /**
   * 页面的初始数据
   */
  data: {
    money: 1.0,
    bulkbuyingpopshow: false,
    episodesshow: false,
    episodesList: [],
    episodes: "1-20",
    indexNumber: 0,
    selectedAllStatus: false,
    infoList: [],
    startpoint: 1,
    maxpage: 10,
    indexNumber: 0,
    count:"0",
    money:"0",
    nomoney:false,
    info: {},
    walletinfo:{}
  },

  tocharge:function(){
    wx.navigateTo({
      url: '/pages/my/my_wallet/my_wallet',
    })
  },
  confirmbuy: function() {
    console.log("======" + seasonsids)
    var vm = this
    var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    var data = {
      "userId": userIds ,
      "programId": id,
      "seasonIds":seasonsids
    }
    getApp().requestLoading(getApp().globalData.https + "orderResource/walletRecordEvery",
      "get", data,"正在购买..",
      function(res) {
        if (res.code == 0) {
          if(res.data.disPrice<0){
            vm.setData({
              nomoney: true,
              walletinfo:res.data
            })
          }else{
            let pages = getCurrentPages();
            var currPage = pages[pages.length - 2]; //上一页
            currPage.setData({
              isrefresh:true
            })
            wx.navigateBack();
          }
        }else{
          getApp().showMsgToast(res.message);
          vm.setData({
            walletinfo: { "disPrice": vm.data.money},
            nomoney: true
          })
        }
      },
      function(res) {});
  },
  pay: function() {
    var vm = this
    //判断是否更新完毕
    if (vm.data.totalResults<vm.data.info.totalSeason){
      getApp().showMsgToast("功能更新中，敬请期待")
      return
    }
    wx.navigateTo({
      url: '../my/pay/pay_sure?info=' + JSON.stringify(vm.data.info),
    })
  },
  /**
   * 获取栏目详情
   */
  getdetails: function(id) {
    var vm = this
    var userIds = wx.getStorageSync("userId") === "" ? -1 : wx.getStorageSync("userId");
    var data = {
      "startPoint": "1",
      "maxPage": "10",
    }
    getApp().request(getApp().globalData.https + "programResource/programDetail/" + id + "/" + userIds,
      "get", data,
      function(res) {
        if (res.code == 0) {
          vm.setData({
            info: res.data,
          })
        }
      },
      function(res) {});
  },


  getdata: function() {
   

    var that = this;
    var data = {
      "programId": id,
    }
    getApp().request(getApp().globalData.https + "programResource/batchSeasons",
      "get", data,
      function(res) {
        if (res.code == 0) {
          that.setData({
            episodesList: res.data.area
          });
        }
      },
      function(res) {
        console.log(res)
      }
    )

  },

  /**
   * 获取单个栏目下所有集数列表
   */
  getalldata: function(id) {
    var vm = this
    console.log(this.data.startpoint)
    var data = {
      "startPoint": this.data.startpoint,
      "maxPage": "50",
      "userId": wx.getStorageSync("userId")
    }
    getApp().requestLoading(getApp().globalData.https + "/programResource/seasons/" + id,
      "get", data, "加载中...",
      function(res) {
        if (res.code == 0) {
          vm.setData({
            infoList: res.data.data,
          })
          var infoList=res.data.data;
          for (var i = 0; i < infoList.length; i++) {
            infoList[i].selected = true;
            vm.setData({
              infoList: infoList,
              totalResults: res.data.totalResults
            });
            vm.calcuate()
          }
        }
      });
  },
  bindSelectAll: function() {
    var that = this;
    var selectedAllStatus = this.data.selectedAllStatus;
    console.log(selectedAllStatus);
    this.setData({
      selectedAllStatus: !selectedAllStatus
    });
    // selectedAllStatus = !selectedAllStatus;
    var infoList = this.data.infoList;
    if (!selectedAllStatus) {
      for (var i = 0; i < infoList.length; i++) {
        infoList[i].selected = selectedAllStatus;
        console.log(infoList[i].selected);
        this.setData({
          infoList: infoList
        });
      }
    } else {
      for (var i = 0; i < infoList.length; i++) {
        infoList[i].selected = selectedAllStatus;
        console.log(infoList[i].selected);
        this.setData({
          infoList: infoList
        });
      }

    }

    this.calcuate()

  },


  bindCheckbox: function(e) {
    var index = e.currentTarget.dataset.index;
    var selected = this.data.infoList[index].selected;
    var infoList = this.data.infoList;
    infoList[index].selected = !selected;
    this.setData({
      infoList: infoList
    });
    this.calcuate()
  },

  episodesShow: function() {
    var that = this
    this.setData({
      episodesshow: !that.data.episodesshow
    })
  },
  episodesHide: function(e) {
    var episodesList = this.data.episodesList;
    var indexNumber = e.currentTarget.dataset.index; //当前操作对象的 index
    var that = this;
    this.setData({
      episodesshow: !that.data.episodesshow,
      episodes: episodesList[indexNumber],
      startpoint: indexNumber + 1,
      maxpage: episodesList[indexNumber].split("-")[1],
      indexNumber: indexNumber
    });

    this.getalldata(id);

  },

  // toggle: function (e) {
  //   var that = this;
  //   var type = that.data.type === '全选本页' ? '取消全选' : '全选本页';
  //   that.setData({
  //     type: type
  //   });
  // },

  bulkBuyingPopShow: function() {
    if (containbuyed){
      getApp().showMsgToast("包含已购买课程")
      return
    }

    if(this.data.count==0){
      getApp().showMsgToast("请选择需要购买的课程")
      return
    }
    var that = this
    this.setData({
      bulkbuyingpopshow: !that.data.bulkbuyingpopshow
    })
  },
  bulkBuyingPopHide: function(e) {
    var that = this
    this.setData({
      bulkbuyingpopshow: !that.data.bulkbuyingpopshow
    })
  },
  calcuate:function(){
    containbuyed=false;
    count=0;
    price=0;
    seasonsids="";
    for (var i = 0; i < this.data.infoList.length; i++) {
      if (this.data.infoList[i].selected) {
        count++;
        price = price + this.data.infoList[i].price
        seasonsids = seasonsids + this.data.infoList[i].id + ",";
        if (this.data.infoList[i].buySelf==1){
            //包含买了
          containbuyed=true;
        }
      }
    }

    seasonsids=seasonsids.substring(0, seasonsids.lastIndexOf(","))

    this.setData({
      count: count,
      money: price.toFixed(2)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    id=options.id;
    this.getdata()
    this.getalldata(id)
    this.getdetails(id)
  },

})